<?php

use Biz\service\trade\UserAddress as sAddressUser;
use Biz\tools\ShopArea as toolsShopArea;
use Doraemon\tools\Log as toolsLog;

/**
 * 30天无访问
 * @deprecated
 */
class UseWxAddressAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REALNAME;
    const DEFAULT_ERROR_CODE = self::API_CODE_ERROR;

    public function main()
    {
        $wxAddress = $this->getRequest()->getPost('wx_address', '');
        if ($wxAddress) {
            $wxAddress = json_decode($wxAddress, true);
            if ($wxAddress && $wxAddress['nationalCode'] && $wxAddress['telNumber'] && $wxAddress['userName'] && $wxAddress['detailInfo'] && $wxAddress['countyName']) {
                $codeId = $wxAddress['nationalCode'];
                $areaInfo = toolsShopArea::getInfoByCode($codeId);
                if ($areaInfo && $areaInfo['province_id'] && $areaInfo['city_id']) {
                    $data = [
                        'uid' => $this->uid,
                        'accept_name' => $wxAddress['userName'],
                        'province' => $areaInfo['province_id'],
                        'city' => $areaInfo['city_id'],
                        'area' => $areaInfo['area_id'],
                        'info_address' => ($areaInfo['area_id'] ? '' : $wxAddress['countyName']) . $wxAddress['detailInfo'],
                        'mobile' => $wxAddress['telNumber']
                    ];
                    $result = sAddressUser::createOrEdit(0, $data);
                    $this->setSuccess($result);
                } else {
                    toolsLog::api_useWxAddress_error('无法使用微信地址,地区code码不匹配或则缺少', [
                        'wx_address' => $wxAddress,
                        'use_result' => $areaInfo
                    ]);
                    throw new Exception('微信地址不存在');
                }
            } else {
                throw new Exception('微信地址信息不全');
            }
        } else {
            throw new \Exception('使用微信地址失败');
        }
    }
}